package edu.ustb.aur.utils;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import edu.ustb.aur.rightpackage.*;

/**
 * @author Yao Bowen
 * @description 定制抽象权利的反序列化,将抽象权利重定向到某个具体的权利上去
 */
public class AbstractRightJsonDeserializer implements JsonDeserializer<AbstractRight> {
    @Override
    public AbstractRight deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        String rightType = jsonElement.getAsJsonObject().get("type").getAsString();
        switch (rightType) {
            case "USE_RIGHT":
                return jsonDeserializationContext.deserialize(jsonElement, UseRight.class);
            case "USUFRUCT_RIGHT":
                return jsonDeserializationContext.deserialize(jsonElement, UsufructRight.class);
            case "POSSESS_RIGHT":
                return jsonDeserializationContext.deserialize(jsonElement, PossessRight.class);
            case "DISPOSE_RIGHT":
                return jsonDeserializationContext.deserialize(jsonElement, DisposeRight.class);
            default:
                throw new IllegalArgumentException("Error Right Type!");
        }
    }
}
